/*
 * TestDumpCCTypeSpeicifer.c
 *
 *  Created on: 2012/05/31
 *      Author: guanxiaoying
 */
#include "TestDump.h"

static VOID dumpTypeSpecifier( AST astTypeSpec ){
	AST astCodeScp = CTypeSpecifier.getCodeScope( astTypeSpec );
	S2 s2Cnt;

	sprintf( tempBuf, header_format, dumpGetStr( XML_RECORD), astTypeSpec );
	dumpOutputStream( tempBuf );

	dumpCodeScopeAsField( astCodeScp );
	/* type  */
	sprintf( tempBuf, sub_tab_header_format, dumpGetStr( XML_TYPE), "",
			CTypeSpecifier.getTypeInfo(astTypeSpec) );
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_TYPE) );
	dumpOutputStream( tempBuf );
	/* declaration  */
	sprintf( tempBuf, sub_tab_header_format, dumpGetStr( XML_DECLARATION), "",
			CTypeSpecifier.getTypeInfo(astTypeSpec) );
	dumpOutputStream( tempBuf );
	sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_DECLARATION) );
	dumpOutputStream( tempBuf );
	/* member  */
	if( CTypeSpecifier.isBasicType( astTypeSpec ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr("basic");
		sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_KIND) );
		dumpOutputStream( tempBuf );
	}else if( CTypeSpecifier.isStruct( astTypeSpec ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr("struct");
		sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_KIND) );
		dumpOutputStream( tempBuf );
	}else if( CTypeSpecifier.isUnion( astTypeSpec ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr("union");
		sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_KIND) );
		dumpOutputStream( tempBuf );
	}else if( CTypeSpecifier.isTypedefIdentifier( astTypeSpec ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr("typedef-id");
		sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_KIND) );
		dumpOutputStream( tempBuf );
	}else if( CTypeSpecifier.isEnum( astTypeSpec ) ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr("enum");
		sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_KIND) );
		dumpOutputStream( tempBuf );
	}else{
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_KIND), "" );
		dumpOutputStream( tempBuf );
		dumpParameterStr(dumpGetStr( XML_ERROR));
		sprintf( tempBuf, sub_tab_tail_format, dumpGetStr( XML_KIND) );
		dumpOutputStream( tempBuf );
	}
	/*storage*/

	s2Cnt = CTypeSpecifier.getAutoNum( astTypeSpec );
	while( s2Cnt > 0 ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FLAG ), "storage-class" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "auto" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FLAG ) );
		dumpOutputStream( tempBuf );
		s2Cnt--;
	}
	s2Cnt = CTypeSpecifier.getExternNum( astTypeSpec );
	while( s2Cnt > 0 ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FLAG ), "storage-class" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "extern" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FLAG ) );
		dumpOutputStream( tempBuf );
		s2Cnt--;
	}

	s2Cnt = CTypeSpecifier.getRegisterNum( astTypeSpec );
	while( s2Cnt > 0 ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FLAG ), "storage-class" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "register" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FLAG ) );
		dumpOutputStream( tempBuf );
		s2Cnt--;
	}
	s2Cnt = CTypeSpecifier.getStaticNum( astTypeSpec );
	while( s2Cnt > 0 ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FLAG ), "storage-class" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "static" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FLAG ) );
		dumpOutputStream( tempBuf );
		s2Cnt--;
	}
	s2Cnt = CTypeSpecifier.getTypedefNum( astTypeSpec );
	while( s2Cnt > 0 ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FLAG ), "storage-class" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "typedef" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FLAG ) );
		dumpOutputStream( tempBuf );
		s2Cnt--;
	}
	/* extend key */
	dumpOutputStream( file_buf );
	s2Cnt = CTypeSpecifier.getAsmNum( astTypeSpec );
	while( s2Cnt > 0 ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FLAG ), "storage-class" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "asm" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FLAG ) );
		dumpOutputStream( tempBuf );
		s2Cnt--;
	}
	s2Cnt = CTypeSpecifier.getInlineNum( astTypeSpec );
	while( s2Cnt > 0 ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FLAG ), "storage-class" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "inline" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FLAG ) );
		dumpOutputStream( tempBuf );
		s2Cnt--;
	}

	s2Cnt = CTypeSpecifier.getInterruptNum( astTypeSpec );
	while( s2Cnt > 0 ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FLAG ), "storage-class" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "interrupt" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FLAG ) );
		dumpOutputStream( tempBuf );
		s2Cnt--;
	}
	s2Cnt = CTypeSpecifier.getNearNum( astTypeSpec );
	while( s2Cnt > 0 ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FLAG ), "storage-class" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "near" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FLAG ) );
		dumpOutputStream( tempBuf );
		s2Cnt--;
	}
	s2Cnt = CTypeSpecifier.getFarNum( astTypeSpec );
	while( s2Cnt > 0 ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FLAG ), "storage-class" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "far" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FLAG ) );
		dumpOutputStream( tempBuf );
		s2Cnt--;
	}
	/* type qualifier */
	s2Cnt = CTypeSpecifier.getConstNum( astTypeSpec );
	while( s2Cnt > 0 ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FLAG ), "storage-class" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "const" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FLAG ) );
		dumpOutputStream( tempBuf );
		s2Cnt--;
	}
	s2Cnt = CTypeSpecifier.getVolatileNum( astTypeSpec );
	while( s2Cnt > 0 ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FLAG ), "storage-class" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "volatile" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FLAG ) );
		dumpOutputStream( tempBuf );
		s2Cnt--;
	}

	s2Cnt = CTypeSpecifier.getRestrictNum( astTypeSpec );
	while( s2Cnt > 0 ){
		sprintf( tempBuf, sub_value_header_format, dumpGetStr( XML_FLAG ), "storage-class" );
		dumpOutputStream( tempBuf );
		dumpParameterStr( "restrict" );
		sprintf( tempBuf, sub_value_tail_format, dumpGetStr( XML_FLAG ) );
		dumpOutputStream( tempBuf );
		s2Cnt--;
	}

	sprintf( tempBuf, tail_format, dumpGetStr( XML_RECORD) );
	dumpOutputStream( tempBuf );
}

VOID DumpTypeSpecifier(  PSZ szPath, PSZ postfix ){
	AST astTypeSpec;
	AST astMax;

	if( szPath == NULL ){
		return;
	}
	strcpy( file_buf, szPath );
	file_buf[strlen(szPath)] = '\0';
	strcat(file_buf, postfix );
	file_buf[strlen(szPath)+strlen(postfix)] = '\0';
	if( COEXIT_SUCCESS ==dumpOutputStreamInit_file(file_buf)){
		astTypeSpec = MIN_ARRAY;
		astMax = CTypeSpecifier.getMax();
		sprintf( tempBuf, top_header, dumpGetStr(XML_TYPE) );
		dumpOutputStream( tempBuf );
		while( astTypeSpec <= astMax ){
			dumpTypeSpecifier( astTypeSpec );
			astTypeSpec ++;
		}
		sprintf( tempBuf, top_end, dumpGetStr(XML_TYPE) );
		dumpOutputStream( tempBuf );
		dumpOutputStreamEnd_file();
	}
}
